<?php
declare(strict_types=1);

namespace App\Service;

use App\Model\Players;
use App\Model\PlayersGift;
use App\Model\UsersGift;
use App\Traits\PagingTrait;
use Hyperf\DbConnection\Db;


/**
 * 笔记服务层
 *
 * @package App\Service
 */
class GiftService extends BaseService
{
    use PagingTrait;


    public static function userGiftRank($activityId, $force = 0, $limit = 4)
    {
        $key = 'LIVE_USER_GIFT_RANK_'.$activityId;
        $r = redis()->get($key);
        info('r', [$r]);
        if (!$r || $force) {
            $r = UsersGift::where('users_gift.activity_id', '=', $activityId)
                ->join('user_detail as u', 'u.user_id', '=', 'users_gift.user_id')
                ->orderBy('users_gift.points', 'desc')
                ->limit($limit)
                ->get(['u.user_id', 'u.nickname', 'u.head_img', 'users_gift.points'])
                ->toArray();
            redis()->set($key, json_encode($r), 3600);
        } else {
            $r = json_decode($r, true);
        }

        return $r;
    }

    public static function PlayerGiftRank($activityId, $force=0, $limit = 4)
    {
        $key = 'LIVE_PLAYER_GIFT_RANK_'.$activityId;
        $r = redis()->get($key);
        info('r', [$r]);
        if (!$r || $force) {
            $r= PlayersGift::where('players_gift.activity_id', '=', $activityId)
                ->join('players as p', 'p.sign_id', 'players_gift.player_id')
                ->orderBy('players_gift.points', 'desc')
                ->limit($limit)
                ->get(['players_gift.player_id', 'p.name','p.head_img', 'players_gift.points']) //
                ->toArray();
            foreach ($r as $k => $v) {
                $img = data_get($v, 'head_img', '');
                if (empty($img)) {
                    $r[$k]['head_img'] = 'https://cdn.rockysports.cn/%E5%BE%AE%E6%AD%A5%E8%AE%A1%E6%97%B6.png';
                }
            }
            redis()->set($key, json_encode($r), 3600);
        } else {
            $r = json_decode($r, true);
        }
        return $r;
    }


    public static function PlayerGiftRankV2($activityId,$user_id = 0)
    {

        $page = 1;
        $pageSize = 20000;
        $model = Players::where('players.activity_id', '=', $activityId)
            ->leftjoin('players_gift as pg', 'pg.player_id', '=', 'players.sign_id')
            ->join('signups as o', 'o.id', '=', 'players.sign_id');

//        $model = PlayersGift::where('players_gift.activity_id', '=', $activityId)
//            ->join('players as p', 'p.sign_id', 'players_gift.player_id');
//            ->leftjoin('user_detail as u', 'u.user_id', '=', 'p.user_id');

        $count = $model->count();

        $rows  = [];
        $idnums = [];
        $namekv = [];
        if ($count > 0) {
//            $model->orderBy('pg.points', 'desc');

            $rows = $model->forPage($page, $pageSize)->get(['players.sign_id as player_id', 'players.head_img', 'players.name', 'pg.points', 'o.id_num','o.mine_id'])->toArray();

            foreach ($rows as $k => $v) {
                $name = data_get($v, 'name');
                $idNum = data_get($v, 'id_num');
                $namekv[$idNum] = $name;
            }

            foreach ($rows as $k => $v) {
                $points = data_get($v, 'points', 0);
                $playerId = data_get($v, 'player_id', 0);
                if (!data_get($v, 'points')) {
                    $rows[$k]['points'] = 0;
                }

                $headImg = data_get($v, 'head_img', 'https://cdn.rockysports.cn/%E5%BE%AE%E6%AD%A5%E8%AE%A1%E6%97%B6.png');
                if (empty($headImg)) {
                    $headImg = 'https://cdn.rockysports.cn/%E5%BE%AE%E6%AD%A5%E8%AE%A1%E6%97%B6.png';
                }
                $idnum = data_get($v, 'id_num', '');
                $follow_status = 1;
                $is_fans = 0;
                // 关注状态
                if ($user_id) {
                    $mine_id = data_get($v, 'mine_id', 0);
                    $follow_user = DB::table('user_follow')
                        ->where('user_id',$user_id)
                        ->where('to_user_id',$mine_id)
                        ->first();
                    if (!empty($follow_user)) {
                        $follow_status = 2;
                    }
                    $to_follow_user = DB::table('user_follow')
                        ->where('to_user_id',$user_id)
                        ->where('user_id',$mine_id)
                        ->first();
                    if (!empty($to_follow_user)) {
                        $is_fans = 1;
                    }
                    if ($follow_status == 2) {

                        if (!empty($to_follow_user)) {
                            $follow_status = 3;
                        }
                    }
                }
                if (!empty($idnum)) {
                    if (!isset($idnums[$idnum])) {
                        $idnums[$idnum]['points'] = 0;
                        $idnums[$idnum]['mine_id'] = data_get($v, 'mine_id', '');
                        $idnums[$idnum]['player_id'] = $playerId;
                        $idnums[$idnum]['head_img'] = $headImg;
                        $idnums[$idnum]['follow_status'] = $follow_status;
                        $idnums[$idnum]['is_fans'] = $is_fans;
                        $idnums[$idnum]['name'] = data_get($v, 'name');
                    }
                    $idnums[$idnum]['points'] += $points;
                } else {
                    $idnums[$k]['points'] = empty($points)? 0: $points;
                    $idnums[$k]['mine_id'] = data_get($v, 'mine_id', '');
                    $idnums[$k]['player_id'] = $playerId;
                    $idnums[$k]['head_img'] = $headImg;
                    $idnums[$k]['is_fans'] = $is_fans;
                    $idnums[$k]['follow_status'] = $follow_status;
                    $idnums[$k]['name'] = data_get($v, 'name');
                }
            }

//            print_r($idnums);exit;
            $ages = [];
            foreach ($idnums as $user) {
                $ages[] = $user['points'];
            }
            array_multisort($ages, SORT_DESC, $idnums);
            $idnums = array_values($idnums);
            $order = 0;
            foreach ($idnums as $k => $v) {
                $order ++;
                $idnums[$k]['order'] = $order;
            }
//            print_r($idnums);
        }
//return ;

        $redis = new \Redis();
        $redis->connect(env('REDIS_HOST', 'localhost'), (int) env('REDIS_PORT', 6379));
        $redis->auth(env('REDIS_AUTH', null));
        $redis->del('LIVE_PLAYER_RANK_V2_'.$activityId);
        $pipe=$redis->multi($redis::PIPELINE);

        foreach ($idnums as $k => $v) {
            $pipe->rPush('LIVE_PLAYER_RANK_V2_'.$activityId, json_encode($v));
        }

        $replies = $pipe->exec();


    }




}
